package com.train.daily.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.train.common.resp.PageResp;
import com.train.daily.dto.seat.DailyTrainSeatEditChoiceSeatDTO;
import com.train.daily.dto.seat.DailyTrainSeatPageDTO;
import com.train.daily.dto.seat.DailyTrainSeatQueryListDTO;
import com.train.daily.dto.seat.DailyTrainSeatCountDTO;
import com.train.daily.po.DailyTrainSeatPO;
import com.train.daily.resp.seat.DailyTrainSeatPageResp;
import com.train.daily.resp.seat.DailyTrainSeatQueryListResp;
import com.train.mybatis.base.MyBaseMapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【dailyTrainSeat(每日座位)】的数据库操作Mapper
 * @createDate 2025-03-01 23:55:05
 * @Entity generator.domain.DailyTrainSeat
 */
public interface DailyTrainSeatMapper extends MyBaseMapper<DailyTrainSeatPO> {

    default PageResp<DailyTrainSeatPageResp> pageList(DailyTrainSeatPageDTO dailyTrainSeatPageDTO) {
        LambdaQueryWrapper<DailyTrainSeatPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(dailyTrainSeatPageDTO.getCode()), DailyTrainSeatPO::getTrainCode, dailyTrainSeatPageDTO.getCode())
                .eq(ObjectUtil.isNotNull(dailyTrainSeatPageDTO.getDate()), DailyTrainSeatPO::getDate, dailyTrainSeatPageDTO.getDate())
                .orderBy(true, true, DailyTrainSeatPO::getTrainCode)
                .orderBy(true, true, DailyTrainSeatPO::getCarriageIndex)
                .orderBy(true, true, DailyTrainSeatPO::getCarriageSeatIndex);
        PageResp<DailyTrainSeatPO> pageResp = this.selectPage(dailyTrainSeatPageDTO, queryWrapper);
        List<DailyTrainSeatPageResp> respList = BeanUtil.copyToList(pageResp.getData(), DailyTrainSeatPageResp.class);
        return new PageResp<>(pageResp.getTotal(), respList);
    }

    default boolean deleteTrains(LocalDate parse, List<String> trainCodes) {
        return this.delete(new LambdaQueryWrapper<DailyTrainSeatPO>()
                .eq(DailyTrainSeatPO::getDate, parse)
                .in(DailyTrainSeatPO::getTrainCode, trainCodes)) > 0;
    }

    default boolean existsSeatByTrain(LocalDate parse, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainSeatPO> queryWrapper = new LambdaQueryWrapper<DailyTrainSeatPO>()
                .eq(DailyTrainSeatPO::getDate, parse)
                .in(DailyTrainSeatPO::getTrainCode, trainCodes)
                .last("limit 1");
        return this.exists(queryWrapper);
    }

    default List<DailyTrainSeatQueryListResp> selectTrainSeatList(DailyTrainSeatQueryListDTO dto) {
        LambdaQueryWrapper<DailyTrainSeatPO> queryWrapper = new LambdaQueryWrapper<DailyTrainSeatPO>()
                .select(DailyTrainSeatPO::getId, DailyTrainSeatPO::getCarriageIndex,
                        DailyTrainSeatPO::getRow, DailyTrainSeatPO::getCol,
                        DailyTrainSeatPO::getCarriageSeatIndex, DailyTrainSeatPO::getSell,
                        DailyTrainSeatPO::getSeatType)
                .eq(DailyTrainSeatPO::getDate, dto.getDate())
                .eq(DailyTrainSeatPO::getTrainCode, dto.getTrainCode())
                .in(DailyTrainSeatPO::getCarriageIndex, dto.getCarriageIndexList());
        List<DailyTrainSeatPO> dailyTrainSeatPOS = this.selectList(queryWrapper);
        return BeanUtil.copyToList(dailyTrainSeatPOS, DailyTrainSeatQueryListResp.class);
    }

    default boolean batchEditDailyTrainSeat(DailyTrainSeatEditChoiceSeatDTO choiceSeat) {
        LambdaUpdateWrapper<DailyTrainSeatPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DailyTrainSeatPO::getSell, choiceSeat.getSell())
                .eq(DailyTrainSeatPO::getId, choiceSeat.getId());
        return this.update(updateWrapper) > 0;
    }

    List<DailyTrainSeatCountDTO> countDailyTrainSeat(@Param("date") LocalDate date, @Param("trainCode") String trainCode);
}